package com.ximu.test.base;

import java.util.Random;
import java.lang.reflect.Method;

/**
 * 利用反射的机制,可以动态的实例化类和执行类中的方法
 * @author justup@qq.com
 */
public class Reflection {

	public static void main(String[] args){
		Random random = new Random();
		String className = "";
		switch(random.nextInt(2)+1){
		case 1:
			className = "com.ximu.test.base.TestA";
			break;
		case 2:
			className = "com.ximu.test.base.TestB";
			break;
		}
		try{
			Class<?> newClass = Class.forName(className);
		 	Method m = newClass.getMethod("run");
		 	m.invoke((Object)newClass.newInstance());
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
}

abstract class Test{
	abstract public void run();
}


class TestA extends Test{
	public void run() {
		System.out.println("Class TestA");
	}
}

class TestB extends Test{
	public void run() {
		System.out.println("Class TestB");
	}
}
